package com.yhd.exception;


import com.yhd.model.LoginUserModel;
import com.yhd.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@ControllerAdvice
@Slf4j
public class MyExceptionResolver {

    @ExceptionHandler(value = Exception.class)
    public void defaultHandler(HttpServletRequest request, Exception ex, HttpServletResponse response, Model model)throws Exception{
        LoginUserModel jwtUserModel = (LoginUserModel) request.getAttribute("jwtUser");
        if(jwtUserModel!=null){
            log.error(ex.toString(),ex);
        }
        ex.printStackTrace();
        model.addAttribute("exception",ex);
        model.addAttribute("url",request.getRequestURL());
        if(Utils.isAjax(request)){
            response.sendRedirect("/index/notfound");
        }else {
            response.sendRedirect("/index/error500");
        }
    }

}
